package com.aurora.source.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aurora.source.R;
import com.aurora.source.util.ErrorImgUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: ww
 * @description:添加图片适配器
 * @date: 2018/9/19
 */
public class AddImgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<File> files = new ArrayList<>();
    private static final int TYPE_ADD = 1;
    private static final int TYPE_IMG = 2;
    private OnAddImgClickListener onAddImgClickListener;

    public AddImgAdapter(Context context) {
        this.context = context;
    }

    public void setOnAddImgClickListener(OnAddImgClickListener onAddImgClickListener) {
        this.onAddImgClickListener = onAddImgClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == files.size()) {
            return TYPE_ADD;
        } else {
            return TYPE_IMG;
        }
    }

    public interface OnAddImgClickListener {
        void onAddImgClick();
    }

    public void addItem(File file){
        files.add(file);
        notifyDataSetChanged();
    }

    public void addAll(List<File> files){
        this.files.addAll(files);
        notifyDataSetChanged();
    }

    public List<File> getFiles(){
        return files;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        if (viewType == TYPE_ADD) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_add_img, parent, false);
            holder = new AddImgHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_img, parent, false);
            holder = new ImageHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_ADD) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onAddImgClickListener != null) {
                        onAddImgClickListener.onAddImgClick();
                    }
                }
            });
        } else {
            final ImageHolder imageHolder = (ImageHolder) holder;
            Glide.with(context).load(files.get(position))
                    .apply(new RequestOptions().placeholder(R.drawable.ic_default).error(ErrorImgUtil.getErrorImgRandom()))
                    .into(imageHolder.ivImg);
            imageHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    files.remove(imageHolder.getLayoutPosition());
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return files.size() + 1;
    }

    public class ImageHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        public ImageHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public class AddImgHolder extends RecyclerView.ViewHolder {

        public AddImgHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
